package com.exam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.exam.exception.ClassesIsExistException;
import com.exam.exception.MajorIsExistException;
import com.exam.exception.StudentNonExist;
import com.exam.pojo.Major;
import com.exam.pojo.MajorAndClasses;
import com.exam.pojo.Paging;
import com.exam.pojo.Student;
import com.exam.service.ClassesService;
import com.exam.service.MajorService;
import com.google.gson.Gson;

/**
 * @author 郭荣锋
 * 专业管理控制器
 */
@Controller
@RequestMapping("/MajorManage")
public class MajorManage {

	@Resource(name="mv")
	private ModelAndView mv;
	@Resource(name="majorServiceImpl")
	private MajorService majorService;
	@Resource(name="gson")
	private Gson gson;
	@Resource(name="classesServiceImpl")
	private ClassesService classesService;
	
	@RequestMapping("/major-list")
	public ModelAndView major_list(HttpServletRequest request,HttpServletResponse response) {
		//获取所有的专业和班级
		List<MajorAndClasses> list = majorService.getAllMajorAndClasses(); 
		String s = gson.toJson(list);
		mv.addObject("majorAndClasses",s);
		mv.setViewName("admin/major-list");
		return mv;
	}
	
	/**
	 * 显示已删除的专业列表
	 * @param request
	 * @param respone
	 * @return
	 */
	@RequestMapping("/major-deleteList")
	public ModelAndView major_deleteList(HttpServletRequest request,HttpServletResponse response) {
		
		mv.setViewName("admin/major-deleteList");
		return mv;
	}
	/**
	 * 添加专业
	 * @throws IOException 
	 */
	@RequestMapping("/addMajor")
	public void addMajor(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String majorName = request.getParameter("majorName");
		try {
			majorService.addMajor(majorName);
			printWriter.write("{\"res\":\"1\"}");
		} catch (MajorIsExistException e) {
			printWriter.write("{\"res\":\"0\"}");		
		}
		printWriter.close();
	}
	
	/**
	 * 更新专业
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/updateMajor")
	public void updateMajor(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String newName = request.getParameter("newName"); //新名称
		String majorName = request.getParameter("majorName"); //原有名称
		try {
			majorService.updateMajor(majorName, newName);
			printWriter.write("{\"res\":\"1\"}");
		} catch (MajorIsExistException e) { //出现重名异常
			printWriter.write("{\"res\":\"0\"}");			
		}
		printWriter.close();
	}
	
	/**
	 * 添加班级
	 * @param request
	 * @param response 
	 * @throws IOException IO异常
	 */
	@RequestMapping("/updateClasses")
	public void updateClasses(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String majorName = request.getParameter("majorName");
		String newName = request.getParameter("newName"); //新名称
		String classesName = request.getParameter("classesName");
		try {
			classesService.updateClasses(majorName, newName, classesName);
			printWriter.write("{\"res\":\"1\"}");
		} catch (ClassesIsExistException e) {
			printWriter.write("{\"res\":\"0\"}");
		}
		printWriter.close();
	}
	
	/**
	 * 添加班级
	 * @throws IOException 
	 */
	@RequestMapping("/addClasses")
	public void addClasses(HttpServletRequest request,HttpServletResponse response) throws IOException {	
		PrintWriter printWriter = response.getWriter();
		String majorName = request.getParameter("majorName");
		String classesName = request.getParameter("classesName");
		try {
			classesService.addClasses(majorName, classesName);
			printWriter.write("{\"res\":\"1\"}");		
		} catch (ClassesIsExistException e) { // 名称已存在
			printWriter.write("{\"res\":\"0\"}");		
		}
		printWriter.close();
	}
	
	/**
	 * 删除班级 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/deleteClasses")
	public void deleteClasses(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String majorName = request.getParameter("majorName");
		System.out.println("majorName"+majorName);
		String classesName = request.getParameter("classesName");
		System.out.println("classesName"+classesName);
		classesService.deleteClasses(majorName, classesName);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 删除专业
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/deleteMajor")
	public void deteleMajor(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String majorName = request.getParameter("majorName");
		majorService.deleteMajor(majorName);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 分页获取已经被删除的专业
	 * @throws IOException 
	 */
	@RequestMapping("/getDeletedMajorByPaging")
	public void getDeletedMajorByPaging(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		//获取已删除的专业
		Integer page = Integer.parseInt(request.getParameter("page"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		Paging<Major> p = majorService.getDeletedMajorByPaging(page, pageSize);
		String s = gson.toJson(p);
		printWriter.write(s);
		printWriter.close();
	}
	
	/**
	 * 批量恢复专业
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/recoverMajorByQuery")
	public void recoverMajorByQuery(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		//将专业编号列表传到Service层
		System.out.println("执行批量恢复专业");
		String list = request.getParameter("majorIdList");
		majorService.recoverMajorByQuery(list);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 恢复专业
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/recoverMajor")
	public void recoverMajor(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		System.out.println("执行恢复专业");
		Integer majorId = Integer.parseInt(request.getParameter("majorId"));
		majorService.recoverMajor(majorId);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}

}
